<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="样品编号" prop="sampleNo">
        <el-input
          v-model="queryParams.sampleNo"
          placeholder="请输入样品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调查船" prop="surveyShip">
        <el-input
          v-model="queryParams.surveyShip"
          placeholder="请输入调查船"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="航次" prop="voyage">
        <el-input
          v-model="queryParams.voyage"
          placeholder="请输入航次"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="海域" prop="marineArea">
        <el-input
          v-model="queryParams.marineArea"
          placeholder="请输入海域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="占位" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入占位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="x坐标" prop="xCoordinate">
        <el-input
          v-model="queryParams.xCoordinate"
          placeholder="请输入x坐标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="y坐标" prop="yCoordinate">
        <el-input
          v-model="queryParams.yCoordinate"
          placeholder="请输入y坐标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="detailAddress">
        <el-input
          v-model="queryParams.detailAddress"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结束深度" prop="endDepth">
        <el-input
          v-model="queryParams.endDepth"
          placeholder="请输入结束深度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="心长" prop="heartLong">
        <el-input
          v-model="queryParams.heartLong"
          placeholder="请输入心长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存放位置" prop="storageLocation">
        <el-input
          v-model="queryParams.storageLocation"
          placeholder="请输入存放位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['inbound:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['inbound:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['inbound:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['inbound:order:import']"
        >导入</el-button>
<!--        <div class="clearfix">-->
<!--          <span>Excel导入</span>-->
<!--          <el-button-->
<!--            style="float: right; margin-top: -5px"-->
<!--            type="primary"-->
<!--            @click="handleImport"-->
<!--          >-->
<!--            <i class="el-icon-upload2"></i> 导入-->
<!--          </el-button>-->
<!--        </div>-->

      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['inbound:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange" id="orderTable">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="orderNo" />
      <el-table-column label="样品编号" align="center" prop="sampleNo" />
      <el-table-column label="调查船" align="center" prop="surveyShip" />
      <el-table-column label="航次" align="center" prop="voyage" />
      <el-table-column label="详细位置" align="center" prop="locationDetail">
        <el-table-column label="海域" align="center" prop="marineArea" />
        <el-table-column label="占位" align="center" prop="position" />
        <el-table-column label="x坐标" align="center" prop="xCoordinate" />
        <el-table-column label="y坐标" align="center" prop="yCoordinate" />
        <el-table-column label="详细地址" align="center" prop="detailAddress" />
      </el-table-column>
      <el-table-column label="结束深度（m）" align="center" prop="endDepth" />
      <el-table-column label="心长（m）" align="center" prop="heartLong" />
      <el-table-column label="存放位置" align="center" prop="storageLocation" />
      <el-table-column label="保存状况" align="center" prop="preservationStatus" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['inbound:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['inbound:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-divider content-position="center">入库单信息</el-divider>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="样品编号" prop="sampleNo">
              <el-input v-model="form.sampleNo" placeholder="请输入样品编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="调查船" prop="surveyShip">
              <el-input v-model="form.surveyShip" placeholder="请输入调查船" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="航次" prop="voyage">
              <el-input v-model="form.voyage" placeholder="请输入航次" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="结束深度" prop="endDepth">
              <el-input
              onkeyup="value=value.replace(/[^\d.]/g,'').replace(/^0+(\d)/,'$1').replace(/^\./g,'0.').replace(/\.(\d{4})\d+/g,'.$1')"
              v-model="form.endDepth"
              placeholder="请输入结束深度" >
                <template slot="append">(cm)</template>
              </el-input>

            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="心长" prop="heartLong">
              <el-input
              onkeyup="value=value.replace(/[^\d.]/g,'').replace(/^0+(\d)/,'$1').replace(/^\./g,'0.').replace(/\.(\d{4})\d+/g,'.$1')"
              v-model="form.heartLong"
              placeholder="请输入心长" >
                <template slot="append">(cm)</template>
              </el-input>

            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="存放位置" prop="storageLocation">
              <el-input v-model="form.storageLocation" placeholder="请输入存放位置" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="海域" prop="marineArea">
              <el-input v-model="form.marineArea" placeholder="请输入海域" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">详细位置信息</el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="占位" prop="position">
              <el-input v-model="form.position" placeholder="请输入占位" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="x坐标" prop="xCoordinate">
              <el-input
              onkeyup="value=value.replace(/[^\d.]/g,'').replace(/^0+(\d)/,'$1').replace(/^\./g,'0.').replace(/\.(\d{4})\d+/g,'.$1')"
              v-model="form.xCoordinate"
              placeholder="请输入x坐标"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="y坐标" prop="yCoordinate">
              <el-input
              onkeyup="value=value.replace(/[^\d.]/g,'').replace(/^0+(\d)/,'$1').replace(/^\./g,'0.').replace(/\.(\d{4})\d+/g,'.$1')"
              v-model="form.yCoordinate"
              placeholder="请输入y坐标"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="详细地址" prop="detailAddress">
              <el-input  v-model="form.detailAddress" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <DataImport ref="DataImportRef" @search="getList"></DataImport>
  </div>
</template>

<script>
import {listOrder, getOrder, delOrder, addOrder, updateOrder} from "@/api/warehouse/inbuond"
import * as XLSX from 'xlsx/xlsx.mjs'
import DataImport from "@/views/warehouse/inbound/components/DataImport.vue";

export default {
  name: "Order",
  components: {DataImport},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sampleNo: null,
        surveyShip: null,
        voyage: null,
        marineArea: null,
        position: null,
        xCoordinate: null,
        yCoordinate: null,
        detailAddress: null,
        endDepth: null,
        heartLong: null,
        storageLocation: null,
        preservationStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    /** 查询入库单列表 */
    getList() {
      this.loading = true
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        orderNo: null,
        sampleNo: null,
        surveyShip: null,
        voyage: null,
        marineArea: null,
        position: null,
        xCoordinate: null,
        yCoordinate: null,
        detailAddress: null,
        endDepth: null,
        heartLong: null,
        storageLocation: null,
        preservationStatus: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderNo)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加入库单"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const orderNo = row.orderNo || this.ids
      getOrder(orderNo).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改入库单"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderNo != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderNos = row.orderNo || this.ids
      this.$modal.confirm('是否确认删除入库单编号为"' + orderNos + '"的数据项？').then(function() {
        return delOrder(orderNos)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('inbound/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    handleImport() {
      this.$nextTick(()=>{
        this.$refs.DataImportRef.reset();
        this.$refs.DataImportRef.dialogVisible = true;
      })
    },
  }
}
</script>
